﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RBT.DataAccess;
using RBT.Models;

namespace RBT.Controllers {
    public class HomeController : Controller {

        public ActionResult Upload()
        {
            return View();
        }

        // This action handles the form POST and the upload
        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Content/Documents"), fileName);
                file.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return RedirectToAction("Upload");
        }

        //SVN Version
        public ActionResult Index()
        {
            ViewBag.Message = "Message from controller";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Location()
        {
            return View();
        }

        public ActionResult Gallery()
        {
            return View();
        }


        public ActionResult ContactUs()
        {
            return View();
        }



        public ActionResult AddEmployeeId()
        {
            return View();
        }

        public ActionResult RemoveEmployee()
        {
            return View();
        }


        [Authorize(Users = "Omid")]
        [HttpPost]
        public ActionResult RemoveEmployee(RemoveEmployee model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;

                //Try to validate EmpId first here and output the status
                DAL.RemoveUser(model.EmployeeId, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    return RedirectToAction("UserRemovalSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "Failed to Remove Employee");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        [Authorize(Users = "Omid")]
        [HttpPost]
        public ActionResult AddEmployeeId(AddEmployeeId model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;

                //Try to validate EmpId first here and output the status
                DAL.AddUserId(model.EmployeeId, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    return RedirectToAction("UserIdAddedSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "Failed to add Employee Id");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        // GET: /Account/ChangePasswordSuccess
        public ActionResult UserIdAddedSuccess()
        {
            return View();
        }

        public ActionResult UserRemovalSuccess()
        {
            return View();
        }
    }
}
